package com.mingmen.supplier.activity;


import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mingmen.supplier.R;
import com.mingmen.supplier.defineview.ClearEditText;
import com.mingmen.supplier.flycotablelayout.flyco.tablayout.SegmentTabLayout;
import com.mingmen.supplier.flycotablelayout.flyco.tablayout.listener.OnTabSelectListener;
import com.mingmen.supplier.fragment.CanCelOrdelFragment;
import com.mingmen.supplier.fragment.DaiPeiSongListFragment;
import com.mingmen.supplier.fragment.DingDanListFragment;
import com.mingmen.supplier.fragment.YiWanChengListFragment;

import com.mingmen.supplier.immersionbar.ImmersionBar;
import com.mingmen.supplier.model.EventBean;
import com.mingmen.supplier.tabuse.ui.SegmentTabActivity;
import com.mingmen.supplier.tabuse.ui.SimpleCardFragment;
import com.mingmen.supplier.util.ToolList;
import com.mingmen.supplier.util.ViewFindUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import okhttp3.Call;
import okhttp3.Response;

public class NewDingDanListActivity extends AppCompatActivity {


    @BindView(R.id.text_seleall)
    TextView text_seleall;

    @BindView(R.id.text_shenhezhong)
    TextView text_shenhezhong;
    @BindView(R.id.text_yitongguo)
    TextView text_yitongguo;
    @BindView(R.id.text_newyitongguo)
    TextView text_newyitongguo;
    @BindView(R.id.layout_all)
    LinearLayout layout_all;
    DingDanListFragment dingDanListFragment;
    DaiPeiSongListFragment daiPeiSongListFragment;
    YiWanChengListFragment yiWanChengListFragment;
    CanCelOrdelFragment canCelOrdelFragment;
    String position = "0";
    @BindView(R.id.linear_return)
    LinearLayout linear_return;
    @BindView(R.id.text_Order)
    TextView text_Order;
    @BindView(R.id.layout_toolbar2)
    RelativeLayout layout_toolbar2;
    @BindView(R.id.layout_toolbar)
    RelativeLayout layout_toolbar;
    @BindView(R.id.linear_return1)
    LinearLayout linear_return1;
    @BindView(R.id.text_order_my)
    TextView text_order_my;
    @BindView(R.id.main_search)
    ClearEditText main_search;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newdingdanlist);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();


        position = getIntent().getStringExtra("position");
        text_Order.setText("新订单列表");
        if (position.equals("0")) {
            text_seleall.setBackground(getResources().getDrawable(R.color.normal_bluetextcolor));
            text_seleall.setTextColor(getResources().getColor(R.color.white));
            text_shenhezhong.setBackground(getResources().getDrawable(R.color.white));
            text_shenhezhong.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));
            text_yitongguo.setBackground(getResources().getDrawable(R.color.white));
            text_yitongguo.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));
            text_newyitongguo.setBackground(getResources().getDrawable(R.drawable.shape_white_normal_bgright));
            text_newyitongguo.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));
            layout_all.setBackground(getResources().getDrawable(R.drawable.shape_blue_bluewhitenormal));
            text_Order.setText("新订单列表");
            initFragment1();
        } else if (position.equals("1")) {
            initFragment2();
            text_shenhezhong.setBackground(getResources().getDrawable(R.color.normal_bluetextcolor));
            text_shenhezhong.setTextColor(getResources().getColor(R.color.white));

            text_seleall.setBackground(getResources().getDrawable(R.drawable.shape_white_normal_bgleft));
            text_seleall.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));

            text_yitongguo.setBackground(getResources().getDrawable(R.color.white));
            text_yitongguo.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));
            text_newyitongguo.setBackground(getResources().getDrawable(R.drawable.shape_white_normal_bgright));
            text_newyitongguo.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));
            layout_all.setBackground(getResources().getDrawable(R.drawable.shape_blue_bluewhitenormal));
            text_Order.setText("待配送列表");
        } else if (position.equals("2")) {
            text_yitongguo.setTextColor(getResources().getColor(R.color.white));
            text_yitongguo.setBackground(getResources().getDrawable(R.color.normal_bluetextcolor));
            text_seleall.setBackground(getResources().getDrawable(R.drawable.shape_white_normal_bgleft));
            text_seleall.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));

            text_shenhezhong.setBackground(getResources().getDrawable(R.color.white));
            text_shenhezhong.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));
//                text_newyitongguo.setBackground(getResources().getDrawable(R.color.white));
            text_newyitongguo.setBackground(getResources().getDrawable(R.drawable.shape_white_normal_bgright));
            text_newyitongguo.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));
            layout_all.setBackground(getResources().getDrawable(R.drawable.shape_blue_bluewhitenormal));
            text_Order.setText("已完成列表");
            initFragment3();
        } else if (position.equals("3")) {
            text_newyitongguo.setBackground(getResources().getDrawable(R.drawable.shape_blue_normal_bgright));
            text_newyitongguo.setTextColor(getResources().getColor(R.color.white));

            text_seleall.setBackground(getResources().getDrawable(R.drawable.shape_white_normal_bgleft));
            text_seleall.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));

            text_shenhezhong.setBackground(getResources().getDrawable(R.color.white));
            text_shenhezhong.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));
            text_yitongguo.setBackground(getResources().getDrawable(R.color.white));
            text_yitongguo.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));
            layout_all.setBackground(getResources().getDrawable(R.drawable.shape_blue_bluewhitenormal));
            text_Order.setText("已取消列表");
            initFragment4();
        }
        main_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString())) {
                    EventBean eventBean = new EventBean();
                    if (position.equals("2")){
                        eventBean.setSendCode(19);
                        eventBean.setSendStr(main_search.getText().toString());
                        EventBus.getDefault().post(eventBean);
                    }else if (position.equals("3")){
                        eventBean.setSendCode(20);
                        eventBean.setSendStr(main_search.getText().toString());
                        EventBus.getDefault().post(eventBean);
                    }

                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(main_search.getWindowToken(), 0);
                } else if (s.toString().length() < 1) {

                } else {

                }


//                startActivityForResult(intent, resurtResurant);
            }
        });
        main_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                EventBean eventBean = new EventBean();
//                Toast.makeText(NewDingDanListActivity.this, "sdf", Toast.LENGTH_SHORT).show();

                if (position.equals("2")){
                    eventBean.setSendCode(17);
                    eventBean.setSendStr(main_search.getText().toString());
                    EventBus.getDefault().post(eventBean);
                }else if (position.equals("3")){
                    eventBean.setSendCode(18);
                    eventBean.setSendStr(main_search.getText().toString());
                    EventBus.getDefault().post(eventBean);
                }


                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);



                return false;
            }
        });
    }

    //显示第一个fragment
    private void initFragment1() {
        text_Order.setText("新订单列表");
        layout_toolbar2.setVisibility(View.GONE);
        layout_toolbar.setVisibility(View.VISIBLE);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (dingDanListFragment == null) {
            dingDanListFragment = new DingDanListFragment();
            transaction.add(R.id.main_frame_layout, dingDanListFragment);
        }
        //隐藏所有fragment
        hideFragment(transaction);
        //显示需要显示的fragment
        transaction.show(dingDanListFragment);
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (dingDanListFragment != null) {
            transaction.hide(dingDanListFragment);
        }
        if (daiPeiSongListFragment != null) {
            transaction.hide(daiPeiSongListFragment);
        }
        if (yiWanChengListFragment != null) {
            transaction.hide(yiWanChengListFragment);
        }
        if (canCelOrdelFragment != null) {
            transaction.hide(canCelOrdelFragment);
        }
    }

    //显示第二个fragment
    private void initFragment2() {
        layout_toolbar2.setVisibility(View.GONE);
        layout_toolbar.setVisibility(View.VISIBLE);
        text_Order.setText("待配送列表");
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (daiPeiSongListFragment == null) {
            daiPeiSongListFragment = new DaiPeiSongListFragment();
            transaction.add(R.id.main_frame_layout, daiPeiSongListFragment);
        }
        //隐藏所有fragment
        hideFragment(transaction);
        //显示需要显示的fragment
        transaction.show(daiPeiSongListFragment);
        transaction.commit();
    }

    //显示第三个fragment
    private void initFragment3() {
        text_Order.setText("已完成列表");
        layout_toolbar2.setVisibility(View.VISIBLE);
        layout_toolbar.setVisibility(View.GONE);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (yiWanChengListFragment == null) {
            yiWanChengListFragment = new YiWanChengListFragment();
            transaction.add(R.id.main_frame_layout, yiWanChengListFragment);
        }
        //隐藏所有fragment
        hideFragment(transaction);
        //显示需要显示的fragment
        transaction.show(yiWanChengListFragment);
        transaction.commit();
    }

    //显示第三个fragment
    private void initFragment4() {
        text_Order.setText("已取消列表");
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (canCelOrdelFragment == null) {
            canCelOrdelFragment = new CanCelOrdelFragment();
            transaction.add(R.id.main_frame_layout, canCelOrdelFragment);
        }
        //隐藏所有fragment
        hideFragment(transaction);
        //显示需要显示的fragment
        transaction.show(canCelOrdelFragment);
        transaction.commit();
        layout_toolbar2.setVisibility(View.VISIBLE);
        layout_toolbar.setVisibility(View.GONE);
    }


    @OnClick({R.id.text_seleall, R.id.text_shenhezhong, R.id.text_yitongguo, R.id.text_newyitongguo, R.id.linear_return, R.id.linear_return1, R.id.main_search})
    public void onViewClicked(View view) {
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) text_seleall.getLayoutParams();
        switch (view.getId()) {
            case R.id.linear_return:
            case R.id.linear_return1:
                finish();
                break;
            case R.id.text_seleall:
                layoutParams.setMargins(0, 0, 0, 0);
//                text_seleall.setBackground(getResources().getDrawable(R.drawable.shape_blue_normal_bg));
                position = "0";
                text_seleall.setBackground(getResources().getDrawable(R.color.normal_bluetextcolor));
                text_seleall.setTextColor(getResources().getColor(R.color.white));
                text_shenhezhong.setBackground(getResources().getDrawable(R.color.white));

                text_shenhezhong.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));

                text_yitongguo.setBackground(getResources().getDrawable(R.color.white));
                text_yitongguo.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));
                text_newyitongguo.setBackground(getResources().getDrawable(R.drawable.shape_white_normal_bgright));
                text_newyitongguo.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));

                layout_all.setBackground(getResources().getDrawable(R.drawable.shape_blue_bluewhitenormal));
                initFragment1();
                break;
            case R.id.text_shenhezhong:
                position = "1";
                text_shenhezhong.setBackground(getResources().getDrawable(R.color.normal_bluetextcolor));
                text_shenhezhong.setTextColor(getResources().getColor(R.color.white));

                text_seleall.setBackground(getResources().getDrawable(R.drawable.shape_white_normal_bgleft));
                text_seleall.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));

                text_yitongguo.setBackground(getResources().getDrawable(R.color.white));
                text_yitongguo.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));
                text_newyitongguo.setBackground(getResources().getDrawable(R.drawable.shape_white_normal_bgright));
                text_newyitongguo.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));
                layout_all.setBackground(getResources().getDrawable(R.drawable.shape_blue_bluewhitenormal));
                initFragment2();
                break;
            case R.id.text_yitongguo:
//                layoutParams.setMargins(12,12,12,12);
//                text_yitongguo.setBackground(getResources().getDrawable(R.drawable.shape_blue_normal_bg));
                position = "2";
                text_yitongguo.setTextColor(getResources().getColor(R.color.white));
                text_yitongguo.setBackground(getResources().getDrawable(R.color.normal_bluetextcolor));
                text_seleall.setBackground(getResources().getDrawable(R.drawable.shape_white_normal_bgleft));
                text_seleall.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));

                text_shenhezhong.setBackground(getResources().getDrawable(R.color.white));
                text_shenhezhong.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));
//                text_newyitongguo.setBackground(getResources().getDrawable(R.color.white));
                text_newyitongguo.setBackground(getResources().getDrawable(R.drawable.shape_white_normal_bgright));
                text_newyitongguo.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));
                layout_all.setBackground(getResources().getDrawable(R.drawable.shape_blue_bluewhitenormal));
                initFragment3();
                break;
            case R.id.text_newyitongguo:
//                layoutParams.setMargins(25,25,25,25);
//                layoutParams.setMargins(12,12,12,12);

                position = "3";
                text_newyitongguo.setBackground(getResources().getDrawable(R.drawable.shape_blue_normal_bgright));
                text_newyitongguo.setTextColor(getResources().getColor(R.color.white));

                text_seleall.setBackground(getResources().getDrawable(R.drawable.shape_white_normal_bgleft));
                text_seleall.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));

                text_shenhezhong.setBackground(getResources().getDrawable(R.color.white));
                text_shenhezhong.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));
                text_yitongguo.setBackground(getResources().getDrawable(R.color.white));
                text_yitongguo.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));
                layout_all.setBackground(getResources().getDrawable(R.drawable.shape_blue_bluewhitenormal));
                initFragment4();
                break;
            case R.id.main_search:

                break;
        }

    }

    public void onEventMainThread(EventBean integer) {


    }

}




